package controller;

import model.User;

import org.zkoss.zk.ui.Component;
import org.zkoss.zk.ui.Executions;
import org.zkoss.zk.ui.Sessions;
import org.zkoss.zk.ui.select.SelectorComposer;
import org.zkoss.zk.ui.select.annotation.Listen;
import org.zkoss.zk.ui.select.annotation.Wire;
import org.zkoss.zk.ui.util.Clients;
import org.zkoss.zul.Label;
import org.zkoss.zul.Messagebox;
import org.zkoss.zul.Popup;
import org.zkoss.zul.Textbox;

import admin.dao.UserDAO;

public class LoginCtrl extends SelectorComposer<Component>{

	private static final long serialVersionUID = 1L;

	@Wire
    private Textbox txUsername;
	
	@Wire
    private Textbox txPassword;
	
	@Wire
	private Label lbHello;
	
	@Listen("onClick = #btSubmit")
    public void login(){
		User user = UserDAO.checkUser(txUsername.getValue(), txPassword.getValue());
		
		if(user == null) {
			//Messagebox.show("Error is pressed", "Error", Messagebox.OK, Messagebox.ERROR);
			Clients.showNotification("Login Failed!", "error", txUsername, "middle_center", 3000);
		} else if (user.getLever() == 3){
			Clients.showNotification("You are not authorized!", "warning", txUsername, "middle_center", 3000);
		} else {			
			Sessions.getCurrent().setAttribute("user", user);
			Executions.sendRedirect("index.jsp");
		}
    }
}
